﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OS9800激活码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }

        //计算激活码
        private void button2_Click(object sender, EventArgs e)
        {
            String dtm = this.textBox1.Text;
            dtm = dtm.Trim();//去掉首尾空格
            if (dtm != "" && dtm.Length > 0)
            {
                this.textBox1.Text = dtm;
                String jhm = this.decEncrypt(dtm, "IDO_djaf");
                this.textBox2.Text = jhm;
            }
            else
            {
                MessageBox.Show("动态码不能为空", "错误提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
                this.textBox1.Text = "";
                this.textBox2.Text = "";
            }
        }

        //DEC加密字符串
        private String decEncrypt(String val,String key)
        {
                byte[] rgbKey = Encoding.UTF8.GetBytes(key);
                //rgbIV与rgbKey可以不一样，这里只是为了简便，读者可以自行修改
                byte[] rgbIV = Encoding.UTF8.GetBytes(key);
                byte[] inputByteArray = Encoding.UTF8.GetBytes(val);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
        }
    }
}
